package com.major.crowd.mvc.controller;

import com.github.pagehelper.PageInfo;
import com.major.crowd.constant.CrowdConstant;
import com.major.crowd.entity.Admin;
import com.major.crowd.service.api.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class AdminController {
    @Autowired
    private AdminService adminService;

    //更新
    @RequestMapping("admin/update.html")
    public String update(
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("keyword") String keyword,
            Admin admin
    ){
        adminService.update(admin);
        return "redirect:/admin/get/page.html?pageNum="+pageNum+"&keyword="+keyword;
    }

    //前往修改
    @RequestMapping("admin/to/edit/page.html")
    public String toEditPage(
            @RequestParam("adminId") Integer id,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("keyword") String keyword,
            ModelMap modelMap
    ){
        //查询出
        Admin admin = adminService.queryById(id);
        //存入对象
        modelMap.addAttribute("admin",admin);
        return "admin-edit";
    }

    //新增
    @RequestMapping("admin/save.html")
    public String addAdmin(Admin admin){
        adminService.saveAdmin(admin);
        return "redirect:/admin/get/page.html?pageNum="+Integer.MAX_VALUE;
    }

    //单个删除
    @RequestMapping("admin/remove/{adminId}/{pageNum}/{keyword}.html")
    public String deleteById(
            @PathVariable("adminId") Integer adminId,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("keyword") String keyword
    ){
        int count = adminService.deleteById(adminId);
        System.out.println(count);
        return "redirect:/admin/get/page.html?pageNum="+pageNum+"&keyword="+keyword;
    }

    //分页
    @RequestMapping("/admin/get/page.html")
    public String getPageInfo(
            //defaultValue,默认值为空
            @RequestParam(value = "keyword",defaultValue = "") String keyword,
            //默认值为1
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            //默认值为5
            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
            ModelMap modelMap
    ){
        //调service
        PageInfo<Admin> pageInfo = adminService.getPageInfo(keyword, pageNum, pageSize);
        //将pageInfo对象传入模型
        modelMap.addAttribute(CrowdConstant.ATTR_NAME_PAGE_INFO,pageInfo);
        return "admin-page";
    }

    //登录    (被SpringSecurity接管)
    /*@RequestMapping("admin/do/login.html")
    public String doLogin(
            @RequestParam("loginAcct") String loginAcct,
            @RequestParam("userPswd") String userPswd,
            HttpSession httpSession
    ){
        //如果返回admin对象说明登录成功； 不正确会抛异常
        Admin admin = adminService.getAdminByLoginAcct(loginAcct,userPswd);

        //将登录成功的admin对象存入session域
        httpSession.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN,admin);
        return "redirect:/admin/to/main/page.html";
    }*/

    //登出
    @RequestMapping("/admin/do/logout.html")
    public String doLogout(HttpSession session){
        session.invalidate();
        return "redirect:/admin/to/login/page.html";
    }
}
